package com.yijiaganxi.services.web.delivery;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yijiaganxi.common.entity.delivery.YjgxDelivery;
import org.yijiaganxi.common.res.R;
import org.yijiaganxi.common.vo.web.delivery.YjgxDeliveryWebVo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.services.service.delivery.YjgxDeliveryService;

import java.math.BigDecimal;
import java.util.List;

import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 配送费管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-30
 */
@Validated
@RestController
@RequestMapping("/services/yjgxDelivery/"+GlobalConstants.WEB_FLAG)
public class YjgxDeliveryController {

	
	@Autowired
	public YjgxDeliveryService  deliveryService;
	
	@GetMapping("selectAllDeliveryList")
	public R selectAllDeliveryList() {
		List<YjgxDelivery> selectList = deliveryService
		.selectList(new EntityWrapper<YjgxDelivery>());
		
		if(CollectionUtils.isEmpty(selectList))
			return R.opertionListResponse(selectList);
		
		else {
			YjgxDeliveryWebVo deliveryWebVo;
			List<YjgxDeliveryWebVo>list=Lists.newArrayList();
			for(YjgxDelivery deliveryItem: selectList) {
				deliveryWebVo = (YjgxDeliveryWebVo) YjgxDeliveryWebVo.builder()
				.createDate(deliveryItem.getCreateDate())
				.distance(deliveryItem.getDistance())
				.id(deliveryItem.getId())
				.maxDistance(deliveryItem.getMaxDistance())
				.minDistance(deliveryItem.getMinDistance())
				.money(deliveryItem.getMoney())
				.status(deliveryItem.getStatus())
				.type(deliveryItem.getType()).build();
				if(deliveryItem.getType()==1) {
					deliveryWebVo.setTitle(deliveryItem.getDistance()+"km以下");
				}else if(deliveryItem.getType()==2) {
					deliveryWebVo.setTitle(deliveryItem.getMinDistance()+"km - "+deliveryItem.getMaxDistance()+"km");
				}else if (deliveryItem.getType()==3) {
					deliveryWebVo.setTitle(deliveryItem.getDistance()+"km以上");
				}
				list.add(deliveryWebVo);
			}
			return R.opertionListResponse(list);
		}
				
	}
	
	
	/**
	 * 查询配送详情
	 * @param id
	 * @return
	 */
	@GetMapping("selectDeliveryDetailById")
	public R selectDeliveryDetailById(@NotBlank(message="id必填")String id) {
		return R.opertionPojoResponse(deliveryService.selectById(id));
	}

	
	/**
	 * 添加配送金额信息
	 * @param type
	 * @param money
	 * @param status
	 * @param distance
	 * @param minDistance
	 * @param maxDistance
	 * @return
	 */
	@PostMapping("addOrUpdateDeliveryDetail")
	public R addOrUpdateDeliveryDetail(@NotBlank(message="距离类型必填")Integer type,
			@NotBlank(message="费用必填")String money,
			@NotBlank(message="状态必填")Integer status,
			String distance,String minDistance,String maxDistance,String id) {
		
		if((type==0 || type==2) && StringUtils.isBlank(distance)) {
             return R.error("距离必填");
		}
		
		if((type==1) && StringUtils.isBlank(minDistance)) {
            return R.error("最小距离必填");
		}
		
		if((type==1) && StringUtils.isBlank(maxDistance)) {
            return R.error("最大距离必填");
		}
		
		YjgxDelivery delivery=YjgxDelivery.builder()
				.status(status)
				.type(type)
				.money(new BigDecimal(money))
				.build();
		
		if(type==0 || type==2)
			delivery.setDistance(new BigDecimal(distance));
		
		if(type==1) {
			delivery.setMinDistance(new BigDecimal(minDistance));
			delivery.setMaxDistance(new BigDecimal(maxDistance));
		}
		if(StringUtils.isNotBlank(id)) {
			delivery.setId(Long.valueOf(id));
			return R.insertOrUpdateResponse(deliveryService.updateById(delivery));
		}
		return R.insertOrUpdateResponse(deliveryService.insert(delivery));		
	}
	
	/**
	 * 删除配送金额
	 * @param id
	 * @return
	 */
	@PostMapping("deleteDeliveryById")
	public R deleteDeliveryById(@NotBlank(message="id不能为空")String id) {
		return R.insertOrUpdateResponse(deliveryService.deleteById(id));
	}
	
}

